package org.wwb.antbox.center.web.controller.passport;

import org.ant.commons.commons.context.CommonContext;
import org.ant.commons.commons.context.ContextParam;
import org.ant.commons.models.result.BaseResult;
import org.ant.model.common.session.SessionUser;
import org.ant.shiro.jwt.TokenManagerShiro;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.wwb.antbox.center.web.Router;
import org.wwb.antbox.user.iservice.ServiceUserTool;
import org.wwb.antbox.user.model.passport.model.PwdEditModel;
import org.wwb.antbox.user.model.user.model.SysUserEditModel;
import org.wwb.antbox.user.model.user.view.UserInfoView;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @Author Bond
 * @Date: 2019/9/23 11:31
 * @Description: 登录相关控制器
 */
@RestController
public class PassportController {

    @Autowired
    private TokenManagerShiro tokenManagerShiro;

    /**
     * 获取当前登录账户的个人信息
     * @return BaseResult<UserInfoView>
     */
    @GetMapping(value = Router.PassPort.GET_LOGIN_USER_INFO)
    public BaseResult<UserInfoView> getUserInfo(){
        BaseResult baseResult = new BaseResult();

        ContextParam contextParam = CommonContext.get();
        SessionUser sessionUser = (SessionUser) contextParam.getUserInfo();

        UserInfoView loginUserInfoView = UserInfoView.getUserInfoView(sessionUser);

        baseResult.setModel(loginUserInfoView);
        baseResult.setSuccess(true);
        return baseResult;
    }

    /**
     * 退出
     */
    @PostMapping(value = Router.PassPort.LOGOUT)
    public BaseResult logout(HttpServletRequest request){
        String token = request.getHeader("Authorization");
        BaseResult baseResult = new BaseResult();
        tokenManagerShiro.delCurrentUser(token);
        baseResult.setSuccess(true);
        return baseResult;
    }



    /**
     * 修改系统用户
     * @param sysUserEditModel
     * @return BaseResult<Integer>
     */
    @PostMapping(value = Router.PassPort.EDIT_LOGIN_USER)
    public BaseResult<Integer> editLoginUser(@RequestBody @Valid SysUserEditModel sysUserEditModel){

        BaseResult<Integer> baseResult = ServiceUserTool.sysUserService().editSysUser(sysUserEditModel);

        // 更新Redis
        SessionUser sessionUser = tokenManagerShiro.getCurrentUser();
        sysUserEditModel.getSessionUser(sysUserEditModel,sessionUser);
        tokenManagerShiro.saveCurrentUser(sessionUser.getLoginName(),sessionUser);

        // 更新上下文
        CommonContext.get().setUserInfo(sessionUser);

        return baseResult;
    }


    /**
     * 修改登录账户密码
     * @param pwdEditModel
     * @return BaseResult<Integer>
     */
    @PostMapping(value = Router.PassPort.EDIT_LOGIN_PWD)
    public BaseResult<Integer> editLoginPwd(@RequestBody @Valid PwdEditModel pwdEditModel){
        return ServiceUserTool.passportService().editLoginPwd(pwdEditModel);
    }
}
